package com.example.sociamedia.interaction.controller;

import com.example.sociamedia.interaction.dto.FollowRequest;
import com.example.sociamedia.interaction.dto.FollowResponse;
import com.example.sociamedia.interaction.entity.Follow;
import com.example.sociamedia.user.mapper.UserMapper;
import com.example.sociamedia.user.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/follow")
public class FollowController {
    private final FollowService followService;
    private final UserMapper userMapper;

    @Autowired
    public FollowController(FollowService followService, UserMapper userMapper) {
        this.followService = followService;
        this.userMapper = userMapper;
    }

    @PostMapping("")
    public ResponseEntity<FollowResponse> follow(@RequestHeader("Authorization") String sessionToken, @RequestBody FollowRequest request) {
        FollowResponse resp = followService.followUser(sessionToken, request);
        return new ResponseEntity<>(resp, resp.isSuccess() ? HttpStatus.OK : HttpStatus.BAD_REQUEST);
    }

    @DeleteMapping("")
    public ResponseEntity<FollowResponse> unfollow(@RequestHeader("Authorization") String sessionToken, @RequestBody FollowRequest request) {
        FollowResponse resp = followService.unfollowUser(sessionToken, request);
        return new ResponseEntity<>(resp, resp.isSuccess() ? HttpStatus.OK : HttpStatus.BAD_REQUEST);
    }

    @GetMapping("/followings/{userId}")
    public ResponseEntity<List<User>> getFollowings(@PathVariable Long userId) {
        List<Follow> followings = followService.getFollowings(userId);
        List<User> users = followings.stream()
            .map(f -> userMapper.selectUserById(f.getFollowingId()))
            .filter(u -> u != null)
            .collect(java.util.stream.Collectors.toList());
        return ResponseEntity.ok(users);
    }

    @GetMapping("/followers/{userId}")
    public ResponseEntity<List<User>> getFollowers(@PathVariable Long userId) {
        List<Follow> followers = followService.getFollowers(userId);
        List<User> users = followers.stream()
            .map(f -> userMapper.selectUserById(f.getFollowerId()))
            .filter(u -> u != null)
            .collect(java.util.stream.Collectors.toList());
        return ResponseEntity.ok(users);
    }

    @GetMapping("/check")
    public ResponseEntity<?> checkFollowStatus(@RequestHeader("Authorization") String sessionToken, @RequestParam("targetUserId") Long targetUserId) {
        com.example.sociamedia.user.entity.User user = followService.getUserByToken(sessionToken);
        if (user == null) {
            return new ResponseEntity<>("无效token", HttpStatus.UNAUTHORIZED);
        }
        boolean followed = followService.isFollowing(user.getUserid(), targetUserId);
        return ResponseEntity.ok(java.util.Collections.singletonMap("followed", followed));
    }
} 